package com.customer.controller.customer;

import com.alibaba.fastjson.JSON;
import com.aop.SysLog;
import com.customer.model.Customer;
import com.customer.service.customer.CustomerService;
import com.customer.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerAdmin")
public class CustomerAdminController {

    @Autowired
    private CustomerService customerService;

    // 设置跳转到客户管理首页
    @RequestMapping("/main")
    public String main() {
        return "/customer/main.html";
    }

    // 设置跳转到客户管理首页
    @RequestMapping("/login")
    public String logining() {
        return "/customer/customerLogin.html";
    }

    // 设置跳转客户个人信息页面
    @RequestMapping("/customerInfo")
    public String customerInfo() {
        return "/customer/customerInfo.html";
    }

    // 设置跳转客户个人信息页面
    @RequestMapping("/customerUpdate")
    public String customerUpdate() {
        return "/customer/customerUpdate.html";
    }

    @SysLog(operation = "查询全部用户", method = "showCustomer", module = "后端管理")
    @ResponseBody
    @RequestMapping(value = "/showCustomer", produces = "text/html;charset=utf-8")
    public String showCustomer(int page, int limit, @RequestParam(defaultValue = "", required = true) String phoneNumber) {
        ResponseData responseData = customerService.findAllByPage(page, limit, phoneNumber);
        return JSON.toJSONString(responseData);


//        ResponseData responseData = customerService.findAll();
//        System.out.println("-----------");
//        System.out.println(responseData.getData());
//        return JSON.toJSONString(responseData);
    }

    @RequestMapping("/customerShow")
    public String customerShow() {
        return "/customer/customerShow.html";
    }

    @SysLog(operation = "删除用户", method = "deleteCustomer", module = "后端管理")
    @ResponseBody
    @RequestMapping("/deleteCustomer")
    public String deleteCustomer(int id) {
        ResponseData responseData = customerService.deleteCustomer(id);
        return JSON.toJSONString(responseData);
    }

    @ResponseBody
    @RequestMapping("/update")
    @SysLog(operation = "查询修改用户", method = "update_showCustomer", module = "后端管理")
    // 无法将参数注入到对象中?
//    public String update(int id , String passengerName , String education , String birthday , String gender, String phone ){
    public String update(Customer customer) {
        System.out.println(customer);
        ResponseData responseData = customerService.update(customer);
        return JSON.toJSONString(responseData);
    }

    @ResponseBody
    @RequestMapping("/updateShow")
    @SysLog(operation = "单个用户修改", method = "update_id", module = "后端管理")
    public String updateShow(@RequestParam int id) {
        ResponseData responseData = customerService.updateShow(id);
        return JSON.toJSONString(responseData);
    }
}
